import flask
from flask import Flask, json, request
import fun1
from flask_cors import CORS
from answer_search_ekg import *
from question_classifier_ekg import *
from question_parser_ekg import *
from py2neo import Graph
from Goods_Related import *

api = Flask(__name__)
CORS(api, supports_credentials=True)



# sdk接口
# search
@api.route('/search/sdk/<words>', methods=['GET'])
def get_api1_sdk(words):
    print(words)
    words = words.encode('utf-8').decode('unicode_escape')
    print(words)
    classifier = QuestClassifier()
    parser = QuestParser()
    searcher = AnswerSearcher()
    other = []
    res_classify = classifier.classify(words)
    print(res_classify)
    res_sql = parser.parser_main(res_classify)
    print(res_sql)
    final_answers = searcher.search_main(res_classify, res_sql)
    print(final_answers)
    return final_answers


# relate
@api.route('/relate/sdk/<words>', methods=['GET'])
def get_api2_sdk(words):
    print(words)
    words = words.encode('utf-8').decode('unicode_escape')
    print(words)
    final_answers = goods_related(words)
    l = {}
    other = []
    l.update({"res":final_answers})
    l.update({"other":other})
    print(final_answers)
    return str(l)


# personal
@api.route('/personal/sdk/<words>', methods=['GET'])
def get_api3_sdk(words):
    print(words)
    words = words.encode('utf-8').decode('unicode_escape')
    print(words)
    final_answers = goods_related(words)
    l = {}
    other = []
    l.update({"res":final_answers})
    l.update({"other":other})
    print(final_answers)
    return str(l)


# 普通测试接口
# 普通测试search
@api.route('/search/<words>', methods=['GET'])
def get_api1(words):
    print(words)
    classifier = QuestClassifier()
    parser = QuestParser()
    searcher = AnswerSearcher()
    other = []
    res_classify = classifier.classify(words)
    print(res_classify)
    res_sql = parser.parser_main(res_classify)
    print(res_sql)
    final_answers = searcher.search_main(res_classify, res_sql)
    print(final_answers)
    return str(final_answers)


# 普通测试relate
@api.route('/relate/<words>', methods=['GET'])
def get_api2(words):
    print(words)
    final_answers = goods_related(words)
    l = {}
    other = []
    l.update({"res":final_answers})
    l.update({"other":other})
    print(final_answers)
    return str(l)


# 普通测试personal
@api.route('/personal/<words>', methods=['GET'])
def get_api3(words):
    print(words)
    classifier = QuestClassifier()
    parser = QuestParser()
    searcher = AnswerSearcher()
    other = []
    res_classify = classifier.classify(words)
    print(res_classify)
    res_sql = parser.parser_main(res_classify)
    print(res_sql)
    final_answers = searcher.search_main(res_classify, res_sql)
    print(final_answers)
    return str(final_answers)

# 测试
@api.route('/test/<words>', methods=['GET'])
def get_api4(words):
    print(words)
    words = words.encode('utf-8').decode('unicode_escape')
    print(words)
    dict = {}
    l1 = ["套装", "手机"]
    l2 = []
    dict.update({"res":l1})
    dict.update({"other":l2})
    l = str(dict)
    return l


if __name__ == '__main__':
    api.run()